package com.shanghaionstar.activity.webview.handler;


import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.webkit.WebView;

import com.onstar.cn.common.exception.HttpException;
import com.onstar.cn.sqlite.entities.BroadcastHeaderEntity;
import com.shanghaionstar.user.iview.IBroadcastDetailView;
import com.shanghaionstar.user.presenter.BroadcastDetailPresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.weight.TopLayout;

/**
 * Created by Rengui Lu on 2017/3/3.
 */
public class BroadcastDetailWebHandler extends DefaultWebHandler implements IBroadcastDetailView {
    private BroadcastDetailPresenter detailPresenter;

    public BroadcastDetailWebHandler(Context context) {
        super(context);
    }

    @Override
    public void handMsg(Message msg) {
        switch (msg.what) {
            case IWebType.TYPE_BROADCAST_DETAIL:
                handleDetailContent(msg);
                break;
            case IWebType.TYPE_ON_PAGE_FINISHED:
                //
                if (context instanceof CommonWebViewActivity) {
                    CommonWebViewActivity mActivity = (CommonWebViewActivity) context;
                    TopLayout topLayout = mActivity.getTop_layout();
                    topLayout.setTopTitle(" ");
                }
                break;
        }
    }

    private void handleDetailContent(Message msg) {
        CommonWebViewActivity mActivity;
        if (context instanceof CommonWebViewActivity) {
            mActivity = (CommonWebViewActivity) context;
            Bundle bundle = mActivity.getBundle();
            TopLayout topLayout = mActivity.getTop_layout();
//            String title = ((WebDataEntity) msg.obj).getTopTitle();
            String title = "　";
            topLayout.setTopTitle(title);
//            mActivity.getWebview().setupMySettings();
            String msgId = bundle.getString("message_id");
            detailPresenter = new BroadcastDetailPresenter(mActivity.getScheduler(), mActivity);
            detailPresenter.setBroadcastDetailView(this);
            mActivity.setLoading(true);
            detailPresenter.loadMsgs(msgId);
        }
    }

    @Override
    public void showDetail(BroadcastHeaderEntity response) {
        CommonWebViewActivity mActivity;
        if (context instanceof CommonWebViewActivity) {
            mActivity = (CommonWebViewActivity) context;
            mActivity.setLoading(false);
            WebView webView = mActivity.getWebview();
            if (response != null) {
                if (response.getContent() != null && !"".equals(response.getContent())) {
                    webView.loadData(response.getContent(), "text/html;charset=UTF-8", null);
                    webView.setVisibility(View.VISIBLE);
                } else {
//                    mActivity.getWebview().getSettings().setUseWideViewPort(true);
//                    String url ="https://idt1.onstar.com.cn/mweb/mweb-main/static/OnStar-MA7.0-h5/html/ajx-question/index.html?serviceid=57444&service_time=2017-03-28 11:28:08&service_type=TS&push_date=2017-03-31&vin=LSGZG5378GS026427&location=%25E6%259D%25AD%25E5%25B7%259E";
                    webView.loadUrl(response.getContentUrl());
                    webView.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void showErr(Throwable throwable) {
        CommonWebViewActivity mActivity;
        if (context instanceof CommonWebViewActivity) {
            mActivity = (CommonWebViewActivity) context;
            mActivity.setLoading(false);
            if (throwable instanceof HttpException)
                AlertDialogUtil.showDialogByExceptionCode(mActivity, (HttpException) throwable);
        }
    }
}
